﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MedicWeb.MedRegService;
using MedicWeb.MedicService;

namespace MedicWeb.forms.Editors
{
    public partial class rmrdoctorsearch : SessionPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Form.Action = ResolveUrl("~/forms/Editors/RMRDoctorSearch.aspx");


            //var guid = new Guid("5A4CDD9F-A102-4005-9A96-0A9571BC0F82");

            string id = Request.QueryString["id"];
            string surname = Request.QueryString["surname"];

            if (id != null)
            {
                Form.Action += "?id=" + id + "&surname=" + surname;
                if (string.IsNullOrEmpty(surname)) {
                    surname = "А";
                } else {
                    surname = surname.Replace("_", " ");
                }
                tbRMRSearch.Text = surname;

                if (!IsPostBack && id != "0")
                {
                    var guid = (new MainService()).LoadInstitution(GetSession(), Convert.ToInt32(id)).Rmr;
                    GenerateTable(SetupDataSource(guid, surname, 20));

                }
            }
        }

        protected DataTable SetupDataSource(Guid guid, string surname, int count)
        {

            Session.Remove("ds");
            var mr = new Service1();
            /* if (Session["ds"] == null)
            { */
            var query = mr.GetFlatEmployeeListByLPUandSurname(guid, surname, count);
            //var query = generate(surname);

            // Create the schema of the DataTable.
            DataTable dt = new DataTable();
            DataColumn dc;
            dc = new DataColumn("Id", typeof(Guid)); dt.Columns.Add(dc);
            dc = new DataColumn("Surname", typeof(string)); dt.Columns.Add(dc);
            dc = new DataColumn("Name", typeof(string)); dt.Columns.Add(dc);
            dc = new DataColumn("Patroname", typeof(string)); dt.Columns.Add(dc);
            dc = new DataColumn("Post", typeof(string)); dt.Columns.Add(dc);
            dc = new DataColumn("BeathYear", typeof(int)); dt.Columns.Add(dc);


            // Add rows to the DataTable.
            IEnumerator emplEnum = query.OrderBy(e => e.Surname).GetEnumerator();
            FlatEmployee ne;
            while (emplEnum.MoveNext())
            {
                ne = emplEnum.Current as FlatEmployee;
                DataRow row = dt.NewRow();
                row["Id"] = ne.ID;
                row["Surname"] = ne.Surname;
                row["Name"] = ne.Name;
                row["Patroname"] = ne.Patromane;
                row["Post"] = ne.Post;
                row["BeathYear"] = ne.BearthYear;
                dt.Rows.Add(row);
            }

            Session["ds"] = dt;
            Session["all"] = query;
            Session["lpuguid"] = guid;
            return dt;

            /* }
            else
            {
                return (DataTable)Session["ds"];
            } */
        }

        private void GenerateTable(DataTable dt)
        {

            Table table = tlbRMRDoctor;
            TableRow row = null;

            //Add the Headers
            row = new TableRow();
            /*
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                TableHeaderCell headerCell = new TableHeaderCell();
                headerCell.Text = dt.Columns[j].ColumnName;
                row.Cells.Add(headerCell);
            }
            table.Rows.Add(row);
            */
            //Add the Column values
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                row = new TableRow();
                row.ID = dt.Rows[i][0].ToString();
                //row.Attributes["onmouseover"] = "highlight(this, true);";
                //row.Attributes["onmouseout"] = "highlight(this, false);";
                //row.Attributes["onclick"] = "";
                   
                
                for (int j = 1; j < dt.Columns.Count; j++)
                {
                    TableCell cell = new TableCell();
                    cell.Text = dt.Rows[i][j].ToString();
                    cell.CssClass = "item";
                    row.Cells.Add(cell);
                }
                // Add the TableRow to the Table
                table.Rows.Add(row);
            }
            
        }

        /*
        protected DataTable BindDataSource()
        {
            if (Session["ds"] == null)
            {
                return null;
            }
            return (DataTable)Session["ds"];

        }

        */

        protected void btnRMRSearch_Click(object sender, EventArgs e)
        {
            var txt = tbRMRSearch.Text;
            var dt = (DataTable)Session["ds"];
            var guid = (Guid)Session["lpuguid"];
            if (dt != null & guid != null)
            {
                Session.Remove("ds");
                GenerateTable(SetupDataSource(guid, txt, 0));
            }
        }

        protected void btnSelectRMR_Click(object sender, EventArgs e)
        {
            if (Session["all"] == null | hfRMRDcotorId.Value == null)
            {
                // TODO Add Alert
                return;
            }
            var query = (FlatEmployee[])Session["all"];
            var id = new Guid(hfRMRDcotorId.Value);

            FlatEmployee selectedDoctor = query.Where(d => d.ID == id).Select(d => d).First();
            Session["RMRDoctor"] = selectedDoctor;

            Session.Remove("ds");
            Session.Remove("all");
            Session.Remove("lpuguid");


        }
    }
}